
package com.kugou.game.sdk.ui.widget;

import com.kugou.game.sdk.utils.Rx;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 正在加载(loading)widget
 * 
 * @author chenys
 */
public class LoadingView extends LinearLayout {

    private ImageView mImage;

    private TextView mText;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        setGravity(Gravity.CENTER);
        mImage = new ImageView(context);
        mText = new TextView(context);
        initDrawable(context);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        lp.topMargin = 10;
        addView(mImage, lp);
        addView(mText, lp);
    }

    private void initDrawable(Context context) {
        int drawableId = Rx.drawable.kg_loading;
        mImage.setImageResource(drawableId);
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.VISIBLE) {
            int id = Rx.anim.kg_login_loading;
            if (id != 0) {
                Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(getContext(), id);
                // 使用ImageView显示动画
                mImage.startAnimation(hyperspaceJumpAnimation);
            }
        } else {
            mImage.clearAnimation();
        }
    }

    public void setText(String text) {
        mText.setText(text);
    }
}
